<template>
  <div>
    <el-dialog title="new" :visible.sync="dialogVisible" width="30%" :before-close="handleClose"
               :close-on-press-escape="true">
      <el-form :model="favoriteForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="标题" prop="name">
          <el-input v-model="favoriteForm.title"></el-input>
        </el-form-item>
        <el-form-item label="链接" prop="name">
          <el-input v-model="favoriteForm.url"></el-input>
        </el-form-item>
        <el-form-item label="标签" prop="name">
          <el-input v-model="favoriteForm.tag"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="ok">确 定</el-button>
      </span>
    </el-dialog>
    <el-form :inline="true" :model="queryForm" class="demo-form-inline">
      <el-form-item label="关键词">
        <el-input v-model="queryForm.queryValue" placeholder="请输入要查询的内容"></el-input>
      </el-form-item>
      <el-form-item label="活动区域">
        <el-select v-model="queryForm.queryKey" placeholder="请选择">
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="go">go</el-button>
        <el-button type="primary" @click="dialogVisible = true">add</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="tableData"
              border
              style="width: 100%">
      <el-table-column
        prop="recordId"
        label="ID"
        width="50">
      </el-table-column>
      <el-table-column
        prop="url"
        label="URL"
        width="100">
      </el-table-column>
      <el-table-column
        prop="title"
        label="标题">
      </el-table-column>
      <el-table-column
        prop="tag"
        label="标签">
      </el-table-column>
      <el-table-column
        prop="createDate"
        label="创建时间">
      </el-table-column>
    </el-table>
    <div class="block">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page.sync="currentPage1"
        :page-size="page_size"
        layout="total, prev, pager, next"
        :total="total">
      </el-pagination>
    </div>
  </div>
</template>
<script>
  export default {
    name: '',
    data() {
      return {
        favoriteForm: {
          title: '',
          url: '',
          tag: ''
        },
        //
        dialogVisible: false,
        // 表单
        queryForm: {
          queryKey: '',
          queryValue: '',
          page: 1,
          sortType: 1
        },
        // 回显所需要的内容
        tableData: [],
        currentPage1: 1,
        total: 0,
        page_size: 10,
        // 表单所支持的查询项目
        options: [
          {
            value: 'all',
            label: '全模糊查找'
          }, {
            value: 'url',
            label: '按URL查找'
          },
          {
            value: 'title',
            label: '按站点名查找'
          },
          {
            value: 'tag',
            label: '按帐号查找'
          }
        ]
      }
    },
    mounted() {
      this.loadData();
    },
    methods: {
      ok() {
        this.$ajax.get("api", "user", "addFavorites", this.favoriteForm).then(res => {
          this.$message(res.data.msg);
        });
      },
      cancel() {
        this.dialogVisible = false;
      },
      handleClose(done) {
        this.$confirm('确认关闭？')
          .then(_ => {
            done();
          })
          .catch(_ => {
          });
      },
      go() {
        this.loadData();
      }
      ,
      handleSizeChange(val) {
        console.log(`每页 ${val} 条`);
      }
      ,
      handleCurrentChange(val) {
        console.log(val)
        this.loadData(val);
      }
      ,
      /**
       * 加载数据
       * @param page 第几页,当page为给定时,默认值为1
       */
      loadData(page = 1) {
        this.form.page = page;
        this.$ajax.get("api", "user", "getFavorites", this.form).then(res => {
          if (res.data.code == 200) {
            let data = res.data.data;
            this.total = data.total;
            this.tableData = data.rows;
          } else {
            this.$message(res.data.msg);
          }
        });
      }
    }
  }
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>

</style>
